//
//  InquiryMain_TitleSelectMore_View_WX.m
//  ZITHER
//
//  Created by weixin on 2019/3/13.
//  Copyright © 2019 weixn. All rights reserved.
//

#import "InquiryMain_TitleSelectMore_View_WX.h"
@interface InquiryMain_TitleSelectMore_View_WX ()
@property (nonatomic,strong)BaseBtn * lastBtn;

@end
@implementation InquiryMain_TitleSelectMore_View_WX

- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        [self commonInit];
    }
    return self;
}

- (void)commonInit
{
    
    self.frame = CGRectMake(0, 0, kScreenW, kScreenH);
    
    //    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(hide) name:ZY_HideNotification object:nil];
}

- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (void)show
{
    [self addToKeyWindow];
    [self showAnimationWithCompletion:nil];
}

- (void)hide
{
    [self hideAnimationWithCompletion:^(BOOL finished) {
        [self removeFromKeyWindow];
    }];
}

#pragma mark - private method

- (void)addToKeyWindow
{
    if (!self.superview) {
        UIWindow *keyWindow = [UIApplication sharedApplication].keyWindow;
        [keyWindow addSubview:self];
        
        [self addSubview:self.dimBackgroundView];
        [self addSubview:self.TitleSelectMoreBtnView];


    }
}

-(void)creationBtns{

}

- (void)removeFromKeyWindow
{
    if (self.superview) {
        [self removeFromSuperview];
    }
}

- (void)showAnimationWithCompletion:(void (^)(BOOL finished))completion
{
    [UIView animateWithDuration:0.3 animations:^{
        self.dimBackgroundView.alpha = 0.3;
    } completion:completion];
}

- (void)hideAnimationWithCompletion:(void (^)(BOOL finished))completion
{
    [UIView animateWithDuration:0.3 animations:^{
        self.dimBackgroundView.alpha = 0;
    } completion:completion];
}

#pragma mark - getter
- (UIView *)dimBackgroundView
{
    if (!_dimBackgroundView) {
        _dimBackgroundView = [[UIView alloc] init];
        _dimBackgroundView.frame = CGRectMake(0, 0, kScreenW, kScreenH);
        _dimBackgroundView.backgroundColor = [UIColor blackColor];
        _dimBackgroundView.alpha = 0;
        // 添加手势监听
//        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(hide)];
//        [_dimBackgroundView addGestureRecognizer:tap];
        
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] init];
        _dimBackgroundView.userInteractionEnabled = YES;
        [_dimBackgroundView addGestureRecognizer:tap];
        [[tap rac_gestureSignal]subscribeNext:^(__kindof UIGestureRecognizer * _Nullable x) {
            [self hide];
        }];
        
    }
    return _dimBackgroundView;
}


-(BaseView *)TitleSelectMoreBtnView{
    if (!_TitleSelectMoreBtnView) {
        _TitleSelectMoreBtnView = [[BaseView alloc]init];
        [self.dimBackgroundView addSubview:_TitleSelectMoreBtnView];
        _TitleSelectMoreBtnView.backgroundColor = [UIColor whiteColor];
        [_TitleSelectMoreBtnView setCornerRadiusWithRadius:4 isMasks:YES];
        NSArray *arrTitle = @[@"最多货源",@"最多询价",@"价格最低",@"价格最高"];
        NSArray *arrImg = @[@"nav_btn_descend",@"nav_btn_descend",@"nav_btn_ascend",@"nav_btn_descend"];
        for (int i = 0 ; i<4; i++) {
            BaseBtn *btn = [BaseBtn initCustomBtnImageAndTitles:@[arrTitle[i]] images:@[arrImg[i]] color:@[HexColor(0x333333)]];
            if (i == 0) {
                [btn setTitleColor:baseColorTheme forState:UIControlStateNormal];
                [btn setTitleColor:baseColorTheme forState:UIControlStateSelected];
            }
            btn.titleLabel.font = [UIFont fontWithName:@"PingFang-SC-Regular" size:14.f];
            btn.tag = 100+i;
            btn.imageView.contentMode = UIViewContentModeScaleAspectFit;
            [btn sizeToFit];
            btn.titleEdgeInsets = UIEdgeInsetsMake(0, 5, 0, -20);
            btn.imageEdgeInsets = UIEdgeInsetsMake(0, 30, 0, -70);
            [_TitleSelectMoreBtnView addSubview:btn];
            [btn mas_makeConstraints:^(MASConstraintMaker *make) {
                make.centerX.equalTo(self.TitleSelectMoreBtnView.mas_centerX).offset(0);
                make.top.equalTo(self.TitleSelectMoreBtnView.mas_top).offset(10+i*35);
                make.height.mas_equalTo(20);
                make.width.mas_equalTo(60);
            }];
            if (i==0) {
                self.lastBtn = btn;
            }
            @weakify(self);
            [[btn addTag]subscribeNext:^(id  _Nullable x) {
                @strongify(self);
                [btn setTitleColor:baseColorTheme forState:UIControlStateNormal];
                [btn setTitleColor:baseColorTheme forState:UIControlStateSelected];
                [self.lastBtn setTitleColor:HexColor(0x333333) forState:UIControlStateNormal];
                [self.lastBtn setTitleColor:HexColor(0x333333) forState:UIControlStateSelected];
                self.lastBtn = btn;
                if (self.block) {
                    self.block(btn.tag,arrTitle,arrImg);
                }
                [self hide];
            }];
            
            
            
        }
        
    }
    return _TitleSelectMoreBtnView;
}

@end
